package com.zr.web;

import com.alibaba.fastjson.JSON;
import com.zr.pojo.Brand;
import com.zr.pojo.PageBean;
import com.zr.service.BrandService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/selectByPage")
public class SelectByPage extends HttpServlet {
    private BrandService service = new BrandService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 接收 当前页码 和 每页展示条数  url?currentPage=1&pageSize=5
         String _currentPage = req.getParameter("currentPage");
         String _pageSize = req.getParameter("pageSize");
         int currentPage = Integer.parseInt(_currentPage);
         int pageSize = Integer.parseInt(_pageSize);
        //System.out.println(pageSize);
        //System.out.println(currentPage);
        //2. 调用service查询
         PageBean<Brand> pageBean = service.selectByPage(currentPage, pageSize);
         //2. 转为JSON
         String jsonString = JSON.toJSONString(pageBean);
         //3. 写数据
         resp.setContentType("text/json;charset=utf-8");
         resp.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
